<?php

/**
 * This is the model class for table "tbl_users".
 *
 * The followings are the available columns in table 'tbl_users':
 * @property integer $id
 * @property string $name
 * @property string $username
 * @property string $email
 * @property string $password
 * @property string $usertype
 * @property integer $block
 * @property integer $rate
 * @property integer $sendEmail
 * @property string $registerDate
 * @property string $lastvisitDate
 * @property string $activation
 * @property string $forgotten_password_code
 * @property string $params
 *
 * The followings are the available model relations:
 * @property Bidjob[] $bidjobs
 * @property Chat[] $chats
 * @property Comment[] $comments
 * @property Feedback[] $feedbacks
 * @property Feedback[] $feedbacks1
 * @property Inbox[] $inboxes
 * @property Job[] $jobs
 * @property Job[] $jobs1
 * @property News[] $news
 * @property Service[] $services
 * @property UserLogs[] $userLogs
 * @property UserProfiles[] $userProfiles
 * @property UserSkill[] $userSkills
 * @property Usergroups[] $tblUsergroups
 */
class Users extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Users the static model class
	 */
     public $password_repeat;
    public $verifyCode;
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_users';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
     public function rules()
        {
                // NOTE: you should only define rules for those attributes that
                // will receive user inputs.
                return array(
                	array('email', 'unique'),
                        // So sanh pass và re-pass khi dang ky
                        array('password_repeat','compare','compareAttribute'=>'password'),
                                array('password_repeat','safe'), 
                        // So sanh username dang ky voi username da ton tai trong csdl
                        array('username', 'unique'), 
                        array('name, username, email, password, password_repeat, role, block, sendEmail, registerDate, lastvisitDate, activation, forgotten_password_code', 'required'),
                        // Email has to be a valid email address
                        array('email', 'email', 'pattern'=>"/^[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$/"), 
                        array('block, sendEmail', 'numerical', 'integerOnly'=>true),
                        array('name', 'length', 'max'=>255),
                        array('username', 'length', 'min' => 3, 'max'=>150),
                        // Dong hoa chu thuong va chu HOA de save vao CSDL
                        array('username','filter','filter'=>'strtolower'), 
                        array('email, password, activation', 'length', 'max'=>100),
                        array('role', 'length', 'max'=>25),
                        array('forgotten_password_code', 'length', 'max'=>40),
                        array('verifyCode', 'captcha', 'allowEmpty'=>true),
                );
        }

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'bidjobs' => array(self::HAS_MANY, 'Bidjob', 'user_id'),
			'chats' => array(self::HAS_MANY, 'Chat', 'user_id'),
			'comments' => array(self::HAS_MANY, 'Comment', 'user_id'),
			'feedbacks' => array(self::HAS_MANY, 'Feedback', 'user_id_post'),
			'feedbacks1' => array(self::HAS_MANY, 'Feedback', 'user_id_bid'),
			'inboxes' => array(self::HAS_MANY, 'Inbox', 'user_id'),
			'jobs' => array(self::HAS_MANY, 'Job', 'user_id'),
			'jobswin' => array(self::HAS_MANY, 'Job', 'id_winner'),
			'news' => array(self::HAS_MANY, 'News', 'user_id'),
			'services' => array(self::HAS_MANY, 'Service', 'user_id'),
			'userLogs' => array(self::HAS_MANY, 'UserLogs', 'user_id'),
			'userProfile' => array(self::HAS_ONE, 'UserProfiles', 'user_id'),
			'userSkills' => array(self::HAS_MANY, 'UserSkill', 'user_id'),
			'group' => array(self::BELONGS_TO, 'UserUsergroupMap', 'user_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'name' => 'Name',
			'username' => 'Username',
			'email' => 'Email',
			'password' => 'Password',
			'role' => 'Quyền người dùng',
			'block' => 'Block',
			'sendEmail' => 'Send Email',
			'registerDate' => 'Register Date',
			'lastvisitDate' => 'Lastvisit Date',
			'activation' => 'Activation',
			'forgotten_password_code' => 'Forgotten Password Code',
			'params' => 'Params',
		);
	}
    protected function beforeValidate(){
        if ($this->isNewRecord) {
            $this->role = 'member';
            $this->block    = 1;
            $this->sendEmail = 1;
            $this->registerDate = new CDbExpression('NOW()');
            $this->lastvisitDate = $this->registerDate;
            $this->activation = 0;
            $this->forgotten_password_code = rand(1,150000) + "bodd.biz";
        }
        return parent::beforeValidate();
    }
        
        /**
         * perform one-way encrytion on the password before we store it in the database
         */
           protected function afterValidate() {
                parent::afterValidate();
            $this->password = $this->encrypt($this->password);
           }
                
           public function encrypt($value) {
                return md5($value);
           }

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('usertype',$this->usertype,true);
		$criteria->compare('block',$this->block);
		$criteria->compare('sendEmail',$this->sendEmail);
		$criteria->compare('registerDate',$this->registerDate,true);
		$criteria->compare('lastvisitDate',$this->lastvisitDate,true);
		$criteria->compare('activation',$this->activation,true);
		$criteria->compare('forgotten_password_code',$this->forgotten_password_code,true);
		$criteria->compare('params',$this->params,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
    
    
    static function getName($id){
        $name = Users::model()->findByPk($id);
        return $name->name;
    }
	

}